{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **演示0107：数组拼接与拆分**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例1：拼接数组**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **将数组：** $ \\left[\\begin{matrix}1 & 2 & 3 \\\\4 & 5 & 6 \\\\ \\end{matrix}\\right] $ **和** $ \\left[\\begin{matrix}7 & 8 & 9 \\\\10 & 11 & 12 \\\\ \\end{matrix}\\right] $ **组合成：**$ \\left[\\begin{matrix}1 & 2 & 3 \\\\4 & 5 & 6 \\\\7 & 8 & 9 \\\\10 & 11 & 1\\\\ \\end{matrix}\\right] $  \n",
    ">* *np.concatenate*函数，通过指定*axis=0*，将两个数组沿竖直方向(行索引增长的方向)组合拼接\n",
    ">* *np.vstack*函数。按照竖直(vertical)方向拼接\n",
    ">* 将要拼接的数组放在一个*tuple*中，并且这些数组应具有相同的列数\n",
    ">* 支持多个数组的拼接\n",
    ">* 拼接后的数组与原来的数组不共享数据内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a1 = np.array([[1,2,3],[4,5,6]])\n",
    "a2 = np.array([[7,8,9],[10,11,12]])\n",
    "b1 = np.concatenate((a1, a2), axis=0)    # axis=0可以省略\n",
    "b2 = np.vstack((a1, a2))\n",
    "print(b1)\n",
    "print(b2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]\n",
      " [13 14 15]\n",
      " [16 17 18]]\n"
     ]
    }
   ],
   "source": [
    "# 支持多个同shape数组的拼接\n",
    "a3 = np.array([[13,14,15],[16,17,18]])\n",
    "b3 = np.vstack((a1, a2, a3))\n",
    "print(b3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **将数组：** $ \\left[\\begin{matrix}1 & 2 & 3 \\\\4 & 5 & 6 \\\\ \\end{matrix}\\right] $ **和** $ \\left[\\begin{matrix}7 & 8 \\\\9 & 10 \\\\ \\end{matrix}\\right] $ **组合成：**$ \\left[\\begin{matrix}1 & 2 & 3 & 7 & 8 \\\\4 & 5 & 6 & 9 & 10 \\end{matrix}\\right] $  \n",
    ">* *np.concatenate*函数，通过指定*axis=1*，将两个数组沿水平方向(列索引增长的方向)组合拼接\n",
    ">* *np.hstack*函数。按照水平(horizontal)方向拼接\n",
    ">* 拼接的数组应具有相同的行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  7  8]\n",
      " [ 4  5  6  9 10]]\n",
      "[[ 1  2  3  7  8]\n",
      " [ 4  5  6  9 10]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.array([[1,2,3],[4,5,6]])\n",
    "a2 = np.array([[7,8],[9,10]])\n",
    "b1 = np.concatenate((a1, a2), axis=1)\n",
    "b2 = np.hstack((a1, a2))\n",
    "print(b1)\n",
    "print(b2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例2：数组拆分**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **二维数组/矩阵的水平或数值拆分**  \n",
    ">* *split*(指定*axis=0*)或*vsplit*均可用于在竖直方向上(沿着行索引增长的方向)将所有行进行拆分\n",
    ">* *split*(指定*axis=1*)或*hsplit*均可用于在水平方向上(沿着列索引增长的方向)将所有列进行拆分\n",
    ">* 在水平方向上拆分数组，拆分后的各个数组具有相同的列数。如果不能等分列数，则拆分失败\n",
    ">* 在竖直方向上拆分数组，拆分后的各个数组具有相同的行数。如果不能等分行数，则拆分失败\n",
    ">* 拆分后的各个数组，组成一个新的数组。此时将构成一个三维数组，其中每个元素是一个二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[ 1,  2,  3,  4,  5],\n",
      "       [ 6,  7,  8,  9, 10]]), array([[11, 12, 13, 14, 15],\n",
      "       [16, 17, 18, 19, 20]])]\n",
      "[array([[ 1],\n",
      "       [ 6],\n",
      "       [11],\n",
      "       [16]]), array([[ 2],\n",
      "       [ 7],\n",
      "       [12],\n",
      "       [17]]), array([[ 3],\n",
      "       [ 8],\n",
      "       [13],\n",
      "       [18]]), array([[ 4],\n",
      "       [ 9],\n",
      "       [14],\n",
      "       [19]]), array([[ 5],\n",
      "       [10],\n",
      "       [15],\n",
      "       [20]])]\n"
     ]
    }
   ],
   "source": [
    "a= np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]])\n",
    "b= np.split(a, 2, axis=0)    # 在竖直方向上，将所有行拆成2等分，\n",
    "c = np.vsplit(a, 2)\n",
    "d = np.split(a, 5, axis=1)    # 在水平方向上，将所有行拆成5等分，\n",
    "e = np.hsplit(a, 5)\n",
    "print(b)\n",
    "print(d)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
